﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using BLL;
using Entity;

public partial class UserControl_ucMenu : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadFunction();
    }

    public void LoadFunction()
    {
        try
        {

            if (Utils.AdminUtils.UserName == "admin")
            {
                CtrFunction ctrFunction = new CtrFunction();
                List<uspFunctionGetListByAdminRootResult> listFunction = ctrFunction.GetListFunctionByAdminRoot(-1);
                StringBuilder parentFunction = new StringBuilder();
                parentFunction.Append("<ul>");
                foreach (uspFunctionGetListByAdminRootResult FunctionInfo in listFunction)
                {
                    parentFunction.Append("<li>");
                    parentFunction.Append("<a href=\"javascript:;\"><span>").Append(FunctionInfo.Name).Append("</span></a>");
                    List<uspFunctionGetListByAdminRootResult> listFunctionChild = ctrFunction.GetListFunctionByAdminRoot(FunctionInfo.Id);
                    parentFunction.Append("<ul class='children'>");
                    StringBuilder childFunction = new StringBuilder();
                    foreach (uspFunctionGetListByAdminRootResult FunctionInfoChild in listFunctionChild)
                    {
                        if (childFunction.ToString().IndexOf(FunctionInfoChild.Name) < 0)
                        {
                            childFunction.Append("<li>");
                            childFunction.Append("<a href=\"/").Append(FunctionInfoChild.Url).Append("\" target=\"_self\">").Append(FunctionInfoChild.Name).Append("</a>");
                            childFunction.Append("</li>");
                        }
                    }
                    parentFunction.Append(childFunction.ToString());
                    parentFunction.Append("</ul>");
                    parentFunction.Append("</li>");

                }
                parentFunction.Append("</ul>");
                parentFunction.Append("<div style=\"clear: both\"></div>");
                divParentMenu.InnerHtml = parentFunction.ToString();
            }
            else
            {
                CtrFunction ctrFunction = new CtrFunction();
                List<uspFunctionGetListByAdminIdResult> listFunction = ctrFunction.GetListFunctionByAdminID(Utils.AdminUtils.AdminID, -1);
                StringBuilder parentFunction = new StringBuilder();
                parentFunction.Append("<ul>");
                foreach (uspFunctionGetListByAdminIdResult FunctionInfo in listFunction)
                {
                    parentFunction.Append("<li>");
                    parentFunction.Append("<a href=\"javascript:;\"><span>").Append(FunctionInfo.Name).Append("</span></a>");
                    List<uspFunctionGetListByAdminIdResult> listFunctionChild = ctrFunction.GetListFunctionByAdminID(Utils.AdminUtils.AdminID, FunctionInfo.Id);
                    parentFunction.Append("<ul class='children'>");
                    StringBuilder childFunction = new StringBuilder();
                    foreach (uspFunctionGetListByAdminIdResult FunctionInfoChild in listFunctionChild)
                    {
                        if (childFunction.ToString().IndexOf(FunctionInfoChild.Name) < 0)
                        {
                            childFunction.Append("<li>");
                            childFunction.Append("<a href=\"/").Append(FunctionInfoChild.Url).Append("\" target=\"_self\">").Append(FunctionInfoChild.Name).Append("</a>");
                            childFunction.Append("</li>");
                        }
                    }
                    parentFunction.Append(childFunction.ToString());
                    parentFunction.Append("</ul>");
                    parentFunction.Append("</li>");

                }
                parentFunction.Append("</ul>");
                parentFunction.Append("<div style=\"clear: both\"></div>");
                divParentMenu.InnerHtml = parentFunction.ToString();
            }
        }
        catch
        {
        }
    }
}